﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RBAC.Administry.Role
{
    public partial class Privilege : System.Web.UI.Page
    {
        IList<RBAC.Resource> _resource;
        /// <summary>
        /// 获得当前请求的角色所分配的资源清单
        /// </summary>
        protected IList<RBAC.Resource> GetAssignedResource()
        {
            if (_resource == null)
            {
                RBAC.Role role = ParseRequest();
                _resource = role.GetResources().ToList();
            }
            return _resource;
        }

        #region page logic

        /// <summary>
        /// 载入数据
        /// </summary>
        private void LoadData()
        {
            RBAC.Role role = ParseRequest();

            if (role != null)
            {
                // 绑定角色信息
                this.role.Text = role.ID;
                this.display.Text = string.IsNullOrEmpty(role.Display) ? "无" : role.Display;

                BindResourceGrid();
            }
            else
            {
                report.ReportFailure("请求的角色不存在!");
            }
        }
        /// <summary>
        /// 绑定资源清单
        /// </summary>
        private void BindResourceGrid()
        {
            grid.DataSource = RBAC.Resource.LoadAll();
            grid.DataBind();
        }
        /// <summary>
        /// 解析用户请求的账户
        /// </summary>
        private RBAC.Role ParseRequest()
        {
            string id = Request.QueryString["role"];
            if (!string.IsNullOrEmpty(id))
            {
                return RBAC.Role.Load(id);
            }
            return null;
        }
        /// <summary>
        /// 在绑定行时调用，指出是否应该选择“分配”选框
        /// </summary>
        /// <param name="dataItem">绑定到该行的Resource实例</param>
        protected bool IsAssign(object dataItem)
        {
            return GetAssignedResource().Contains((RBAC.Resource)dataItem);
        }

        private void Unassign(string resource)
        {
            RBAC.Role role = ParseRequest();
            role.Unassign(resource);
            role.Save();

            ScriptManager.RegisterStartupScript(scriptUpdatePanel, this.GetType(), "unassigned", "unassigned();", true);
        }

        private void Assign(string resource)
        {
            RBAC.Role role = ParseRequest();
            role.Assign(resource);
            role.Save();

            ScriptManager.RegisterStartupScript(scriptUpdatePanel, this.GetType(), "assigned", "assigned();", true);
        }

        #endregion

        #region event processing

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void assign_Changed(object sender, EventArgs e)
        {
            CheckBox assign = sender as CheckBox;
            GridViewRow row = assign.NamingContainer as GridViewRow;
            string key = grid.DataKeys[row.RowIndex].Value.ToString();

            if (assign.Checked)
            {
                Assign(key);
            }
            else
            {
                Unassign(key);
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            BindResourceGrid();
        }

        #endregion


    }
}
